package com.qiankun.tang.ui.view;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;

import com.common.library.tools.Logger;
import com.common.library.ui.view.CustomVideoView;
import com.qiankun.tang.R;
import com.qiankun.tang.beans.ADBean;


/**
 * @author txf
 * @Title
 * @package com.qiankun.video.ui.view
 * @date 2017/6/9 0009
 */

public class ADVidoVeiw extends FrameLayout implements MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener {
    private ADBean bean1;
    private CustomVideoView mCustomVideoView;
    public ADVidoVeiw(Context context) {
        super(context);
        init();
    }

    public ADVidoVeiw(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ADVidoVeiw(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    public void stopPlayer(){
        mCustomVideoView.stopPlayback();
    }
    public void startPlay(){
        mCustomVideoView.setVideoPath(bean1.getImgUrl());
    }
    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_advideo, this);
        mCustomVideoView = (CustomVideoView) findViewById(R.id.view_advideo_video);
        mCustomVideoView.setZOrderOnTop(true);
        mCustomVideoView.setZOrderMediaOverlay(true);
        mCustomVideoView.setOnPreparedListener(this);
        mCustomVideoView.setOnCompletionListener(this);
        mCustomVideoView.setOnErrorListener(this);
    }
    public void setData(ADBean bean1) {
        this.bean1 = bean1;
        startPlay();
    }
    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();
    }
    @Override
    public void onCompletion(MediaPlayer mp) {
        mp.stop();
        mp.reset();
        mCustomVideoView.setVideoPath(bean1.getImgUrl());
    }
    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        return true;
    }
    @Override
    protected void onDetachedFromWindow() {
        Logger.i("脱离窗口  停止播放");
        stopPlayer();
        super.onDetachedFromWindow();
    }
}
